package com.xmg.dataStructure.tree;

/**
 * 前序  中序  后序 遍历二叉树
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node1 = new HeroNode(2,"吴用");
        HeroNode node2 = new HeroNode(3,"卢俊义");
        HeroNode node3 = new HeroNode(4,"林冲");
        root.setLeft(node1);
        root.setRight(node2);
        node2.setRight(node3);
        tree.setRoot(root);
        System.out.println("前序遍历");
        tree.preOrder();
        System.out.println("中序遍历");
        tree.infixOrder();
        System.out.println("后序遍历");
        tree.postOrder();
    }


}

class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
    }
    public void infixOrder(){
        if(this.root!=null){
            this.root.infixOrder();
        }
    }
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }
    }
}

class HeroNode{
    private int no;
    private String name;

    private HeroNode left;
    private HeroNode right;
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder(){
        System.out.println(this.toString());
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.preOrder();
        }
        System.out.println(this.toString());
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    public void postOrder(){
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this.toString());

    }
}
